package com.qike.baoxiu.controller;

/**
 * @program: baoxiu
 * @author: Eric
 * @create: 2020-05-31 15:15
 */

import org.springframework.stereotype.Controller;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 开启一个监听器类,用户监听客户端操作
 */
@Controller
@ServerEndpoint("/newMessage/{clientId}")
public class NewMessageSocket {

    private volatile Integer clientId;
    /*客户端会话Session*/
    private Session session;
    /*线程安全Set 用于存放每个客户端处理消息的对象，用Set为不重复*/
    private static ConcurrentHashMap<Integer, NewMessageSocket> webSocketSet = new ConcurrentHashMap<>();
    private static CopyOnWriteArraySet<Integer> clientIdSet = new CopyOnWriteArraySet<>();

    /**
     * webSocket 链接建立之后进行调用
     *
     * @param session
     */
    @OnOpen
    public void onOpen(@PathParam("clientId") Integer clientId, Session session) {
        this.session = session;
        //将新建立的连接添加到Set集合里
        webSocketSet.put(clientId, this);
        System.out.println("webSocket 有新的连接建立/现在的连接数量:" + webSocketSet.size());
    }

    /**
     * 连接关闭调用
     */
    @OnClose
    public void onClose() {
        //将旧连接从Set集合清除
        webSocketSet.remove(this);
        System.out.println("webSocket 有连接关闭/现在的连接数量:" + webSocketSet.size());
    }

    /**
     * 收到客户端信息后调用方法
     *
     * @param message
     * @throws IOException
     */
    @OnMessage
    public void onMessage(String message) throws IOException {
        // 群发消息,遍历所有会话对象,轮询发送消息
//        for (NewMessageSocket socket:webSocketSet){
//            socket.session.getBasicRemote().sendText("收到客户端发来信息【群发消息】:"+message);
//        }
    }

    /**
     * 发生错误时调用
     *
     * @param throwable
     */
    @OnError
    public void onError(Throwable throwable) {
        throwable.printStackTrace();
    }

    /**
     * 收到客户端发送消息
     *
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message, Integer uid) throws IOException {
        if (uid != null) {
            for (Map.Entry<Integer, NewMessageSocket> entry : webSocketSet.entrySet()) {
                if (uid.equals(entry.getKey())) {
                    entry.getValue().session.getBasicRemote().sendText(message);
                }
            }
        } else {
            for (Map.Entry<Integer, NewMessageSocket> entry : webSocketSet.entrySet()) {
                entry.getValue().session.getBasicRemote().sendText(message);
            }
//                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
    }

//        for (NewMessageSocket socket:webSocketSet){
//            socket.session.getBasicRemote().sendText(message);
//        }
}


